{:W('Cate/header')}
{:W('Cate/left')}

    <!--内容部分-->
    <div class="content">
        <div class="title">
            <p>客户管理</p>
            <i class="icon-angle-right"></i>
            <p class="active1"><a href="{:U('Client/client')}" style="color:#515974">客户列表</a></p>
            <i class="icon-angle-right"></i>
            <p class="active">客户详情</p>
        </div>
        <div style="width:100%;padding:0 10px;">
            <div class="main_title2">
                <ul class="clearfix">
                    <li>
                        <span>{$_GET['name']}</span> / 客户名称
                    </li>
                    <li style="line-height:20px;">
                        <em style="font-size: 30px;color:#06b216;">{$totnum}</em> / 用户数
                    </li>
                    <li style="line-height:20px;">
                        <em style="font-size: 30px;color:#06b216;">{$_GET['devnum']}</em> / 设备数
                    </li>
                </ul>
            </div>
        </div>
        <div class="admin_list clearfix">
            <div class="main">
                <div class="main_list_menu">
                    <a href="{:U('Client/client_xq',array('companyId'=>$_GET['companyId'],'name'=>$_GET['name'],'usernum'=>$totnum,'devnum'=>$_GET['devnum']))}" class="active">用户详情</a>
                    <a href="{:U('Client/client_xq2',array('companyId'=>$_GET['companyId'],'name'=>$_GET['name'],'devnum'=>$_GET['devnum'],'usernum'=>$totnum))}">设备详情</a>
                    <!--<a href="{:U('Client/client_xq3',array('companyId'=>$_GET['companyId'],'name'=>$_GET['name'],'devnum'=>$_GET['devnum'],'usernum'=>$totnum))}">订单详情</a>-->
                </div>
                <div class="main_list">
                    <div class="main_list_new">
                        <button id="client_xq"><img src="__PUBLIC__/images/add_j.png">新增</button>
                    </div>
                    <div class="main_list_list user_list">
                        <dl>
                            <dt>
                                <span style="width:5%">序号</span>
                                <span style="width:20%;text-align:left;padding-left:20px;">账号</span>
                                <span style="width:15%;text-align:left;padding-left:20px;">用户名</span>
                                <span style="width:15%">手机号</span>
                                <span style="width:15%">邮箱</span>
                                <span style="width:10%">用户分类</span>
                                <span style="width:20%">操作</span>
                            </dt>
                            <volist name="array['accounts']" id='row' key="k">
                            <dd>
                                <span style="width:5%">{$k}</span>
                                <span style="width:20%;text-align:left;padding-left:20px;">{$row['username']}</span>
                                <span style="width:15%;text-align:left;padding-left:20px;">{$row['markname']}</span>
                                <span style="width:15%">{$row['tel']}</span>
                                <span style="width:15%">{$row['email']}</span>
                                <span style="width:10%">
                                <if condition="$row['role'] eq 0">
                                        系统管理员
                                    <elseif condition="$row['role'] eq 1"/>
                                         企业管理员
                                    <elseif condition="$row['role'] eq 2"/>
                                        区域管理员
                                    <elseif condition="$row['role'] eq 3"/>
                                        普通用户
                                </if>
                                </span>
                                <span style="width:20%">
                                    <if condition="$row['role'] eq 1">
                                    <a class="cz" href="javascript:;" data-json='{$row|json_encode}'>重置密码</a>
                                    <a class="xg" href="javascript:;" data-json='{$row|json_encode}'>修改</a>
                                    <a href="javascript:;" onclick="del_user('{$row.id}','{$row.username}')">删除</a>
                                    </if>
                                </span>
                            </dd>
                            </volist>
                        </dl>
                    </div>
                </div>
                <div class="fy" id="page"></div>
            </div>
        </div>
    </div>
    <!--弹框-->
    <!---->
    <div class="hk"></div>
    <div class="add_group client_xq">
        <div class="add_group_title">
            <img src="__PUBLIC__/images/i_guanbi.png" class="icon-remove">新增企业管理员
        </div>
        <div class="add_group_center">
            <ul>
                <li>
                    <span>
                        <i>*</i>账号：
                    </span>
                    <input value="" name="username" maxlength="20" type="text">
                </li>
                <li>
                    <span>
                        <i></i>用户名：
                    </span>
                    <input value="" name="markname" maxlength="20" type="text">
                </li>
                <li>
                    <span>
                        <i>*</i>手机号：
                    </span>
                    <input value="" name="tel" maxlength="11" type="text">
                </li>
                <li>
                    <span>
                        <i></i>邮箱：
                    </span>
                    <input value="" name="email" type="text">
                </li>
                <li>
                    <span>
                        <i>*</i>密码：
                    </span>
                    <input value="" name="passwd" maxlength="30" type="password">
                </li>
                <li>
                    <span>
                        <i>*</i>确认密码：
                    </span>
                    <input value=""  name="cpasswd" maxlength="30" type="password">
                </li>
            </ul>
        </div>
        <div class="add_group_btn">
            <button>确定</button>
            <button>取消</button>
        </div>
    </div>
    <div class="add_group client_xq2">
        <div class="add_group_title">
            <img src="__PUBLIC__/images/i_guanbi.png" class="icon-remove">修改企业管理员
        </div>
        <div class="add_group_center">
            <ul>
                <li>
                    <span>
                        <i>*</i>账号：
                    </span>
                    <input value="" maxlength="20" disabled name="username" type="text">
                </li>
                <li>
                    <span>
                        <i></i>用户名：
                    </span>
                    <input value="" maxlength="20" name="markname" type="text">
                </li>
                <li>
                    <span>
                        <i>*</i>手机号：
                    </span>
                    <input value="" maxlength="11" name="tel" type="text">
                </li>
                <li>
                    <span>
                        <i></i>邮箱：
                    </span>
                    <input value="" name="email" type="text" >
                </li>
            </ul>
        </div>
        <div class="add_group_btn">
            <button>确定</button>
            <button>取消</button>
        </div>
    </div>
    <div class="add_group xg_user_password">
        <div class="add_group_title">
            <img src="__PUBLIC__/images/i_guanbi.png" class="icon-remove">重置密码
        </div>
        <div class="add_group_center">
            <ul>
                <li>
                        <span>
                            <i>*</i>新密码：
                        </span>
                    <input value=""  name="passwd"  type="password">
                </li>
                <li>
                        <span>
                            <i>*</i>确认密码：
                        </span>
                    <input value="" name="cpasswd"  type="password">
                </li>
            </ul>
        </div>
        <div class="add_group_btn">
            <button>确定</button>
            <button>取消</button>
        </div>
    </div>
</body>
<script type="text/javascript">
    layui.use('laypage', function(){
        var laypage = layui.laypage;

        //执行一个laypage实例
        laypage.render({
            elem: 'page'
            ,count: "<?php echo $totnum; ?>"
            ,limit: "<?php echo $pagecount; ?>"
            ,layout: ['prev', 'page', 'next', 'limit', 'skip']
            ,curr:function(){
                var page = location.search.match(/pageid=(\d+)/);
                return page ? page[1] : 1;
            }()
            ,jump: function(obj,first){  //点击页码出发的事件
                if(first!=true){  //是否首次进入页面
                    var companyId = "<?php echo $getdata['companyId']; ?>";
                    var name = "<?php echo $getdata['name']; ?>";
                    var usernum = "<?php echo $getdata['usernum']; ?>";
                    var devnum = "<?php echo $getdata['devnum']; ?>";
                    var _url = "/client/client_xq?pageid="+obj.curr+"&pagecount="+obj.limit+"&companyId="+companyId+'&name='+name+'&usernum='+usernum+'&devnum='+devnum;
                    window.location.href = _url;
                }
            }
        });
    });

    $(document).ready(function () {
        //添加
        getPopup($('#client_xq'),$('.client_xq'));
        //修改
        $('.user_list').find('.xg').each(function () {
            var _this = $(this);
            getPopup(_this,$('.client_xq2'));
        });
        // 重置密码
        $('.user_list').find('.cz').each(function () {
            var _this = $(this);
            getPopup(_this,$('.xg_user_password'));
        });
        // 新增管理员
        $('.client_xq button').eq(0).on('click',function(){
            var _conf = get_data('.client_xq');
            if(!_conf){
                return false;
            }
            _conf['companyid'] = "<?php echo $_GET['companyId']; ?>";
            $.post("{:U('Client/addClient_post')}",_conf,function(data){
                if(data.result == '0'){
                    layer.msg('新增管理员成功！',{icon:1,time:2000},function(){
                        location.reload();
                    });
                } else {
                    layer.msg(data.msg,{icon:2,time:2000});
                }
            })
        })

        // 获取修改用户的信息
        $('.user_list').on('click','.xg,.cz',function(){
            var _json = $(this).attr('data-json');
            _json = JSON.parse(_json);
            if($(this).has('.xg')){
                var client_xq2 = $('.client_xq2');
                client_xq2.find('input[name="username"]').val(_json.username);
                client_xq2.find('input[name="markname"]').val(_json.markname);
                client_xq2.find('input[name="tel"]').val(_json.tel);
                client_xq2.find('input[name="email"]').val(_json.email);
            }
            _uid = _json.id;   // 全局变量
        })

        // 修改用户
        $('.client_xq2 button').eq(0).on('click',function(){
            var _conf = get_data('.client_xq2');
            if(!_conf){
                return false;
            }
            _conf['accountiddst'] = _uid;
            _conf['validDate'] = 0;
            $.post("{:U('Client/modifyClient_post')}",_conf,function(data){
                if(data.result == '0'){
                    layer.msg('修改管理员成功！',{icon:1,time:2000},function(){
                        location.reload();
                    });
                } else {
                    layer.msg(data.msg,{icon:2,time:2000});
                }
            })
        })

        // 重置密码
        $('.xg_user_password button').eq(0).on('click',function(){
            var _passwd = $('.xg_user_password [name="passwd"]').val();
            var _cpasswd = $('.xg_user_password [name="cpasswd"]').val();
            // 验证密码
            var _checkPass = /^\w{6,18}$/;
            if(!_checkPass.test(_passwd)){
                layer.msg('密码由6-18位字母、数字、下划线组成',{icon:2,time:2000});
                return false;
            }
            // 密码一致性
            if(_cpasswd != _passwd){
                layer.msg('确认密码和新密码不一致',{icon:2,time:2000});
                return false;
            }
            $.post("{:U('client/ResetPswd_post')}",{'passwd':_passwd,'accountiddst':_uid},function(data){
                if(data.result == '0'){
                    layer.msg('重置密码成功！',{icon:1,time:2000},function(){
                        close();
                    })
                } else {
                    layer.msg(data.msg,{icon:2,time:2000});
                }
            })
        });

    })

    // 删除用户
    function del_user(obj,name)
    {
        if(obj == ''){
            layer.msg('参数错误',{icon:2,time:2000});
            return;
        }
        var _length = $('.user_list dd').length;
        layer.confirm('<font style="color: red;">*</font> 确认要删除用户'+name+'吗！',function(index){
            $.post("{:U('Client/delClient_post')}",{'accountiddst':obj},function(data){
                if(data.result == '0'){
                    layer.msg('删除企业管理员成功！',{icon:1,time:2000},function(){
                        var _layui_input = $('.layui-laypage-skip .layui-input');
                        if(_length <= 1 && _layui_input.val() > 1){   // 获取上一页
                            _layui_input.val(_layui_input.val()-1);
                            $('#page .layui-laypage-btn').click();
                        } else {   // 刷新当前页
                            location.reload();
                        }
                    });
                } else {
                    layer.msg(data.msg,{icon:2,time:2000});
                }
            })
        });
    }

    // 获取表单数据
    function get_data(obj)
    {
        // 获取数据
        var _input = $(obj).find('input').not('[disabled]');
        var _conf = {};
        _input.each(function(i){
            _conf[_input.eq(i).attr('name')] = _input.eq(i).val();
        })
        // 验证数据
        for(var i in _conf){
            if(_conf[i] == '' && i != 'markname' && i != 'email'){
                layer.msg('请把表单填写完整！',{icon:2,time:2000});
                return false;
            }
            // 用户名限制
            if(i == 'username'){
                var _checkName = /^[a-zA-Z0-9]{1,20}$/;
                if(!_checkName.test(_conf['username'])){
                    layer.msg('账号仅限英文字母和数字',{icon:2,time:2000});
                    return false;
                }
            }
            if(i == 'tel'){   // 验证手机号
                var _checkPhone =  /^1\d{10}$/;
                if(!_checkPhone.test(_conf['tel'])){
                    layer.msg('手机号格式错误！',{icon:2,time:2000});
                    return false;
                }
            }
            
            if(i == 'email' && $.trim(_conf['email'])){   // 验证邮箱
                var _checkEmail =  /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
                if(!_checkEmail.test(_conf['email'])){
                    layer.msg('邮箱格式错误！',{icon:2,time:2000});
                    return false;
                }
            }
            // 密码验证
            if(i == 'passwd'){
                var _checkPass = /^\w{6,18}$/;
                if(!_checkPass.test(_conf['passwd'])){
                    layer.msg('密码由6-18位字母、数字、下划线组成！',{icon:2,time:2000});
                    return false;
                }
            }
            // 密码一致性验证
            if(i == 'cpasswd'){
                if(_conf['passwd'] != _conf['cpasswd']){
                    layer.msg('确认密码和密码不一致！',{icon:2,time:2000});
                    return false;
                }
            }
        }
        return _conf;
    }
</script>
</html>